import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel, QGridLayout, QHBoxLayout, QTextEdit
from PyQt5.QtGui import QColor, QPalette
from PyQt5.QtCore import Qt

class ObjectMethodDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.current_color = QColor(0, 128, 255)
        self.init_ui()
        
    def init_ui(self):
        layout = QVBoxLayout()
        
        # 颜色预览区域
        self.preview_label = QLabel('颜色预览')
        self.preview_label.setAlignment(Qt.AlignCenter)
        self.preview_label.setMinimumHeight(120)
        self.preview_label.setStyleSheet(f"background-color: {self.current_color.name()}; color: white; border: 2px solid black;")
        
        # 详细信息显示
        self.details_label = QLabel()
        self.details_label.setAlignment(Qt.AlignCenter)
        
        # 控制按钮
        object_btn = QPushButton('使用对象方式选择颜色')
        object_btn.clicked.connect(self.use_object_method)
        
        layout.addWidget(self.preview_label)
        layout.addWidget(self.details_label)
        layout.addWidget(object_btn)
        
        self.update_display()
        self.setLayout(layout)
        self.setWindowTitle('对象方式颜色对话框')
        
    def use_object_method(self):
        from PyQt5.QtWidgets import QColorDialog
        
        # 创建颜色对话框对象
        dialog = QColorDialog(self.current_color, self)
        dialog.setWindowTitle("自定义颜色对话框")
        
        # 设置对话框选项
        dialog.setOptions(
            QColorDialog.ShowAlphaChannel |
            QColorDialog.DontUseNativeDialog
        )
        
        # 连接信号
        dialog.currentColorChanged.connect(self.on_color_changed)
        dialog.colorSelected.connect(self.on_color_selected)
        
        # 显示对话框并等待用户操作
        if dialog.exec_() == QColorDialog.Accepted:
            selected_color = dialog.selectedColor()
            self.current_color = selected_color
            self.update_display()
            print(f"用户确认选择: {selected_color.name()}")
        else:
            print("用户取消了颜色选择")
            
    def on_color_changed(self, color):
        """当前颜色改变时的实时回调"""
        # 实时更新预览（但不改变当前颜色）
        self.preview_label.setStyleSheet(f"background-color: {color.name()}; color: white; border: 2px solid black;")
        print(f"颜色实时变化: {color.name()}")
        
    def on_color_selected(self, color):
        """颜色被选择时的回调（用户点击OK）"""
        print(f"颜色被选择: {color.name()}")
        
    def update_display(self):
        """更新显示信息"""
        self.preview_label.setStyleSheet(f"background-color: {self.current_color.name()}; color: white; border: 2px solid black;")
        
        # 计算对比色用于文本显示
        brightness = (self.current_color.red() * 299 + 
                     self.current_color.green() * 587 + 
                     self.current_color.blue() * 114) / 1000
        text_color = "white" if brightness < 128 else "black"
        
        self.preview_label.setStyleSheet(
            f"background-color: {self.current_color.name()}; "
            f"color: {text_color}; "
            f"border: 2px solid black;"
        )
        
        self.details_label.setText(
            f"RGB: ({self.current_color.red()}, {self.current_color.green()}, {self.current_color.blue()})\n"
            f"HEX: {self.current_color.name()}\n"
            f"Alpha: {self.current_color.alpha()}"
        )


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = ObjectMethodDemo()
    window.show()
    sys.exit(app.exec_())
